// pages/logisticsAddInfo/logisticsAddInfo.js
import {save} from "../../api/aftersale.js";
import {getOrder} from "../../api/aftersale";

var app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        formData: {
            orderItemList: [],
            logisticsName: '',
            logisticsNo: '',
            logisticsDate: '',
            logisticsImg: '',
            remark: ''
          },
          readonly: false,
          fileList: []
    },
    /**
     * 输入框改变
     */
    bindInputChange(e) {
        this.setData({
            [`formData.${e.currentTarget.id}`]: e.detail.value
        })
    },
    /**
     * 日期改变
     */
    bindDateChange(e) {
        this.setData({
            [`formData.${e.currentTarget.id}`]: e.detail.value
        })
    },

    /**
     * 返回
     */
    cancelBtn() {
        let _this = this
        wx.navigateBack({
            success: function (res) {
                res.eventChannel.emit('acceptDataFromOpenerPage2', {data: _this.data.formData})
            }
        })
    },

    /**
     * 返回
     */
    goBack() {
        wx.navigateBack()
    },

    formSubmit(e) {
        var flag = this.valid();
        if (flag) {
            let userInfo = app.globalData.userInfo;
            this.setData({
                'formData.customType': 'ELECTRONIC_AUTHORIZED',
                'formData.customUserName': userInfo.nickname,
                'formData.customMobile': userInfo.mobile,
                'formData.customUserId': userInfo.id,
                'formData.customUserAddressDetail': userInfo.addressDetail,
                'formData.dealType': 'send',
                'formData.state': 'SAVE'
            });
            save(this.data.formData).then(res => {
                let {success} = res;
                if (success) {
                    wx.showToast({
                        title: '提交成功',
                        duration: 2000,
                        success: () => {
                            setTimeout(() => {
                                wx.navigateTo({
                                    url: '/pages/home/home'
                                })
                            }, 2000);
                        }
                    })
                }
            })
        }

    },
    valid() {
        if (!this.data.formData.logisticsName) {
            wx.showToast({
                title: '请输入物流公司名称',
                icon: 'none'
            })
            return false;
        }
        if (!this.data.formData.logisticsNo) {
            wx.showToast({
                title: '请填写物流号',
                icon: 'none'
            })
            return false;
        }
        if (!this.data.formData.logisticsDate) {
            wx.showToast({
                title: '请填写寄件时间',
                icon: 'none'
            })
            return false;
        }
        if (!this.data.formData.logisticsImg) {
            wx.showToast({
                title: '请填写物流单图片',
                icon: 'none'
            })
            return false;
        }
        if (!this.data.formData.remark) {
            wx.showToast({
                title: '请填写备注信息',
                icon: 'none'
            })
            return false;
        }
        return true;
    },

    afterRead(event) {
        let _this = this
        wx.showLoading({
            title: '加载中',
        })
        const {file} = event.detail;
        wx.uploadFile({
            url: `${getApp().globalData.baseUrl}/sys/attachment/upload`,
            filePath: file.url,
            name: 'file',
            success(res) {
                let {data, success} = JSON.parse(res.data)
                if (success) {
                    _this.setData({
                        'formData.logisticsImg': data.attachment,
                        fileList: [{url: data.attachment}]
                    })
                }
                wx.hideLoading()
            }
        });
    },
    deleteImg(e) {
        this.setData({
            'formData.logisticsImg': '',
            fileList: []
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        let this_ = this;

        const eventChannel = this.getOpenerEventChannel();

        if (Object.keys(eventChannel).length > 0) {

            eventChannel.on('acceptDataFromOpenerPage', function (data) {
                    if (data.id) {
                        this_.getLoadFormData(data.id);
                        this_.setData(
                            {
                                "readonly": true
                            }
                        )
                    } else {
                        const dataInfo = data.data;
                        this_.setData(
                            {
                                [`formData.orderItemList`]: dataInfo.orderItemList,
                                [`formData.remark`]: dataInfo.remark,
                                fileList: dataInfo.logisticsImg ? [{url: dataInfo.logisticsImg}] : []
                            })
                    }
                }
            )
        }
    },

    getLoadFormData(id) {
        var this_ = this;
        getOrder({id: id}).then(res => {
            let {data, success} = res
            if (success && data.id) {
                Object.keys(data).forEach(function (key) {
                    this_.setData(
                        {
                            [`formData.${key}`]: data[key]
                        }
                    )
                });
                this_.setData({
                  fileList: this_.data.formData.logisticsImg ? [{url: this_.data.formData.logisticsImg}] : []
                })
            } else {
                wx.showModal({
                    content: "未找到该订单",
                    showCancel: false,
                })
            }
            wx.hideLoading()
        });
    }
    ,

    /**


     /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    }
    ,

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    }
    ,

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    }
    ,

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    }
    ,

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    }
    ,

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    }
    ,

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
